
5
5
.
.
1
1
6
6
.
.
o
o
n
n
L
L
o
o
n
n
g
g
P
P
r
r
e
e
s
s
s
s
G
G
e
e
s
s
t
t
u
u
r
r
e
e
.onLongPressGesture Modifier allows View to react when User holds finger on top of it.
Provided Closure is called when event finishes (when minimumDuration expires).
Alternative is to use .gesture(LongPressGesture) whose Closure accepts additional Boolean parameter.
Compared to .gesture this Closure has no parameter.
Gestures tutorial contains additional explanations.
Syntax
.onLongPressGesture(minimumDuration: 1, maximumDistance: 1) { print("Event ended") }
.onLongPressGesture { print("Event ended") }
Parameters
The duration in seconds that must elapse before the gesture ends
The maximum distance the event can move before the gesture fails
In the below example circle changes color when you Long Press on it.
TapGesture().onEnded()
struct ContentView: View {
@State var color = true
var body: some View {
Circle()
.fill(color ? Color.green : Color.red)
.frame(width: 50, height: 50)
.onLongPressGesture(minimumDuration: 1, maximumDistance: 1) { //Trailing Closure has no
parameters
self.color.toggle()
}
}
}
Initial color Long Press to change color